package com.itheima.controller;

import com.itheima.service.BlogService;
import com.itheima.service.CommentService;
import com.itheima.service.UserService;
import com.itheima.service.WebVisitService;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
@Api(tags = "首页相关接口")
@CrossOrigin
public class IndexController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private WebVisitService webVisitService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    //获取一年内的文章贡献数
    @GetMapping("/getBlogContributeCount")
    @ApiOperation("获取一年内的文章贡献数")
    public String getBlogContributeCount() {
        Map<String,Object> map=blogService.getBlogContributeCount();
        return ResultUtil.result("success", map);
    }

    //获取每个分类下文章数目
    @GetMapping("/getBlogCountByTag")
    @ApiOperation("获取每个标签下文章数")
    public String getBlogCountByTag() {
        List<Map<String, Object>> blogCountByTag = blogService.getBlogCountByTag();
        return ResultUtil.result("success", blogCountByTag);
    }

    //获取每个分类下文章数目
    @GetMapping("/getBlogCountByBlogSort")
    @ApiOperation("获取每个分类下文章数目")
    public String getBlogCountByBlogSort(){
        List<Map<String, Object>> blogCountByTag = blogService.getBlogCountByBlogSort();
        return ResultUtil.result("success", blogCountByTag);
    }


     //获取最近一周用户独立IP数和访问量
    @GetMapping("/getVisitByWeek")
    @ApiOperation("获取最近一周用户独立IP数和访问量")
    public String getVisitByWeek(){
        Map<String, Object> visitByWeek = webVisitService.getVisitByWeek();
        return ResultUtil.result("success",visitByWeek);
    }

    //首页初始化
    @GetMapping("/init")
    @ApiOperation("首页初始化")
    public String init(){
        Map<String, Object> map = new HashMap<>();
        Integer blogCount = blogService.getBlogCount();
        Integer commentCount = commentService.getCommentCount();
        Integer userCount = userService.getUserCount();
        Integer webVisitCount = webVisitService.getWebVisitCount();
        map.put("blogCount", blogCount);
        map.put("commentCount", commentCount);
        map.put("userCount", userCount);
        map.put("webVisitCount", webVisitCount);

        return ResultUtil.result("success",map);
    }
}
